﻿using PollSystem.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace PollSystem
{
    public partial class ShowVotingResults : System.Web.UI.Page
    {
        protected void Page_PreRender(object sender, EventArgs e)
        {
            PollSystemEntities context = new PollSystemEntities();
            using (context)
            {
                var questionId = Convert.ToInt32(Request.Params["questionId"]);
                if (questionId > 0)
                {
                    Question question = context.Questions
                                                    .Include("Answers")
                                                    .FirstOrDefault(q => q.QuestionId == questionId);

                    this.LiteralQuestion.Text = question.QuestionText;

                    this.ChartVoteResults.DataSource = question.Answers.ToList();
                    this.ChartVoteResults.Series["SeriesVotes"].XValueMember = "AnswerText";
                    this.ChartVoteResults.Series["SeriesVotes"].YValueMembers = "Votes";
                    this.ChartVoteResults.Series["SeriesVotes"].Label = "#PERCENT";
                    this.ChartVoteResults.Series["SeriesVotes"].ChartType = System.Web.UI.DataVisualization.Charting.SeriesChartType.Column;
                    this.DataBind();
                }
                else
                {
                    Response.Redirect("Default.aspx");
                }
            }
        }
    }
}